package com.asion.gankapp.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.asion.gankapp.R;
import com.asion.gankapp.applacation.MyApp;
import com.asion.gankapp.utils.ThemeChangeUtil;

import org.greenrobot.greendao.query.Query;

import java.util.List;

import gl.com.app.User;
import gl.com.app.UserDao;

import static com.asion.gankapp.R.id.input_email;
import static com.asion.gankapp.R.id.input_password;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int REQUEST_SIGNUP = 0;
    private EditText inputEmail;
    private EditText inputPassword;
    private AppCompatButton btnLogin;
    private TextView linkSignup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeChangeUtil.changeTheme(this);
        setContentView(R.layout.activity_login);
        initView();

        btnLogin.setOnClickListener(this);
        linkSignup.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.link_signup:
                Intent intent = new Intent(this, SignupActivity.class);
                startActivityForResult(intent, REQUEST_SIGNUP);
                break;
        }
    }

    public void login() {

        if (!validate()) {
            onLoginFailed();
            return;
        }

        btnLogin.setEnabled(false);

        String email = inputEmail.getText().toString();
        String password = inputPassword.getText().toString();

        Intent intent = new Intent();
        intent.putExtra("name",email);
        intent.putExtra("password",password);

        Query<User> query = MyApp.getUserDao().queryBuilder().where(UserDao.Properties.Name.eq(email)).build();
        List<User> list = query.list();
        if(list.size() != 0){
            User user = list.get(0);
            if (user.getPassword().equals(password)) {
                onLoginSuccess(intent);
            }else{
                onLoginFailed();
            }
        }else {
            onLoginFailed();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SIGNUP) {
            if (resultCode == RESULT_OK) {
                String name = data.getStringExtra("name");
                String password = data.getStringExtra("password");
                inputEmail.setText(name);
                inputPassword.setText(password);
                Toast.makeText(this, "恭喜你注册成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onBackPressed() {
       finish();
    }

    public void onLoginSuccess(Intent intent) {
        btnLogin.setEnabled(true);
        setResult(RESULT_OK, intent);
        finish();
    }

    public void onLoginFailed() {
        Toast.makeText(getBaseContext(), "用户名或密码错误！！", Toast.LENGTH_LONG).show();

        btnLogin.setEnabled(true);
    }

    public boolean validate() {
        boolean valid = true;

        String email = inputEmail.getText().toString();
        String password = inputPassword.getText().toString();

        if (email.isEmpty()) {
            inputEmail.setError("用户名不能为空！");
            valid = false;
        } else {
            inputEmail.setError(null);
        }

        if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
            inputPassword.setError("密码必须位于4-13位之间");
            valid = false;
        } else {
            inputPassword.setError(null);
        }

        return valid;
    }

    private void initView() {
        inputEmail = (EditText) findViewById(input_email);
        inputPassword = (EditText) findViewById(input_password);
        btnLogin = (AppCompatButton) findViewById(R.id.btn_login);
        linkSignup = (TextView) findViewById(R.id.link_signup);

        btnLogin.setOnClickListener(this);
    }
}
